<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客正文</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_content.css">
    <link rel="stylesheet" href="editor.md/css/editormd.preview.min.css" />
    <link rel="stylesheet" href="css/comment.css">
    <script src="js/jquery.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="js/urltools.js"></script>
    <script src="js/sessiontolls.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <span id="userElement" style="display:none;">
            <a href="blog_edit.html">写博客</a>
            <a href="javascript:logout()">注销</a>
        </span>

        <a id="loginElement" href="login.html">登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/avatar.png" class="avtar" alt="">
                <h3 id="author2"></h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
            <button class="comment" id="myBtn" style=" border: none;">评论</button>
        </div>

        <div id="myModal">
            <div class="modal-content">
                  <span class="close">&times;</span>
                  
                <!-- <h3>评论 6</h3>
                    <textarea id="comment-box"></textarea>
                    <div>
                        <input onclick="submitComment()" class="comment-input" value="评论">
                    </div>
                <br>
                <span>匿名用户1：</span>
                <br>
                <p>文章写的真好</p>
                <br>
                <span>匿名用户2：</span>
                <br>
                <p>文章写的真好</p>
                <br>
                <span>匿名用户3：</span>
                <br>
                <p>文章写的真好</p><br><br> -->
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right">
            <div class="blog-content">
                <!-- 博客标题 -->
                <h3 id="title"></h3>
                <!-- 博客时间 -->
                <div  class="date">
                    作者:
                    <span id="author"></span>&nbsp;&nbsp;&nbsp;
                    阅读量:
                    <span id="rcount"></span>&nbsp;&nbsp;&nbsp;
                    发布时间:
                    <span id="createtime"></span>&nbsp;&nbsp;&nbsp;
                </div>
                <!-- 博客正文 -->
                <div id="editorDiv">

                </div>
            </div>
        </div>
    </div>
    <script>
        // 获取模态框元素
        var modal = document.getElementById("myModal");
    
        // 获取按钮元素
        var btn = document.getElementById("myBtn");
    
        // 获取<span>元素，用于关闭模态框
        var span = document.getElementsByClassName("close")[0];
    
        // 点击按钮打开模态框
        btn.onclick = function() {
            modal.style.display = "block";
        }
    
        // 点击<span> (x), 关闭模态框
        span.onclick = function() {
            modal.style.display = "none";
        }
    
        // 点击模态框外部，关闭模态框
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    </script>
    <script type="text/javascript">
            var editormd;
            var id = 0;
            function initEdit(md){
                editormd = editormd.markdownToHTML("editorDiv", {
                markdown : md, // Also, you can dynamic set Markdown text
                // htmlDecode : true,  // Enable / disable HTML tag encode.
                // htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
                });
            }
            function getDetail(){
                id = getParamByKey("id");
                if(id == null || id <= 0 || id==undefined){
                    // alert("参数有误!");
                    return false;
                }
                jQuery.ajax({
                    url:"/art/getdetail",
                    type:"post",
                    data:{
                        "id":id
                    },
                    success:function(res){
                        if(res.code == 200 && res.data!=null ||
                        res.data.id>0){
                            //查询文章成功，展示正文 -拼接
                            jQuery("#title").html(res.data.title);
                            jQuery("#author").html(res.data.username);
                            jQuery("#author2").html(res.data.username);
                            jQuery("#rcount").html(res.data.rcount);
                            jQuery("#createtime").html(res.data.createtime);
                            initEdit(res.data.content);

                        }else{
                            alert("操作失败:"+res.msg);
                        }
                    }
                });
            }
            getDetail();
            //增加阅读量
            function addRcount(){
                jQuery.ajax({
                    url:"/art/addrcount",
                    type:"post",
                    data:{
                        "id":id
                    },
                    success:function(){}
                });
            }
            addRcount();
            function islogin(){
                jQuery.ajax({
                    url:"/user/islogin",
                    type:"post",
                    data:{},
                    success:function(res){
                       if(res.code == 200 && res.data == 1){
                            jQuery("#loginElement").hide();
                            jQuery("#userElement").show();
                       }else{
                        jQuery("#loginElement").show();
                            jQuery("#userElement").hide();
                       } 
                    }
                });
            }
            islogin();
                var comment = "";
                var  id = getParamByKey("id");//获取文章id
            function submitComment(){
                comment = jQuery("#comment-box");
                if(comment.val() == ""){
                    return false;
                }
                if(id==null || id<= 0){
                    alert("非法参数！");
                    return false;
                }
                jQuery.ajax({
                    url:'/comment/submit',
                    type:'post',
                    data:{
                        "article_id":id,
                        "content":comment.val()
                    },
                    success:function(res){
                        if(res.data != null && res.code == 200){
                            location.href = location.href;
                            console.log("评论成功！");
                        }else{
                            alert("请先登录!");        
                            console.log("评论失败!");
                            location.href = location.href;
                        }
                    }
                });
            }
            //初始化评论
            function initComment(){
                id = getParamByKey("id");
                if(id==null || id<= 0){
                //  alert("非法参数！");
                return false;
                }
                jQuery.ajax({
                    url:'/comment/init',
                    type:'post',
                    data:{
                        "article_id":id
                    },
                    success:function(res){
                        var commentListHtml = "";
                        
                        if(res.data.length > 0 && res.code == 200){
                            //填充评论区
                            commentListHtml += '<h3>评论:' +res.data.length+'</h3>';
                            commentListHtml += '<textarea id="comment-box"></textarea>';
                                commentListHtml += ' <div>';    
                                commentListHtml += ' <button onclick="submitComment()" class="comment-input">评论</button>';
                                commentListHtml += ' </div>';
                                commentListHtml += '<br>';
                            for(var i = 0;i<res.data.length;i++){
                                var itm = res.data[i];
                               
                                commentListHtml += '<span>'+itm.username+'&nbsp;</span>';
                                commentListHtml += '<span>'+itm.createtime+'</span>';
                                commentListHtml += '<br>';
                                commentListHtml += ' <p>说：'+itm.content+'</p>';
                                commentListHtml += '<br>';
                            }
                            console.log("评论添加成功!");
                        }else{
                                commentListHtml += '<h3>评论:' +0+'</h3>';
                                commentListHtml += '<textarea id="comment-box"></textarea>';
                                commentListHtml += ' <div>';    
                                commentListHtml += ' <button onclick="submitComment()" class="comment-input">评论</button>';
                                commentListHtml += ' </div>';
                                commentListHtml += '<br>';
                        }
                        jQuery(".modal-content").html(commentListHtml);
                    }
                });
            }
            initComment();
    </script> 
</body>

</html>